package com.example.demo.service;

import lombok.SneakyThrows;
import org.springframework.data.redis.connection.FutureResult;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.AsyncResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.concurrent.*;

@Service
public class UserAsyncService {
    public static final ExecutorService pool = new ThreadPoolExecutor(1,
            1,
            0L, TimeUnit.MILLISECONDS,
            new LinkedBlockingQueue<>(250));

    @SneakyThrows
    public String asyncString() {
        Future<String> submit = pool.submit(() -> {
            Thread.sleep(3000L);
            return "执行结果";
        });
        return submit.get();
    }

    @SneakyThrows
    public void asyncVoid() {
        pool.submit(() -> {
            try {
                Thread.sleep(3000L);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        });
    }

    @Async
    public Future<String> asyncString2() {
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        return new AsyncResult<>("执行结果");
    }

    @Async
    public void asyncVoid2() {
        try {
            Thread.sleep(3000L);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}
